#include "weather.h"
#include "ui_weather.h"
#include<QDebug>
#include <QPalette>
#include <QColor>


weather::weather(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::weather)
{
    ui->setupUi(this);
 //创建一个网络访问管理对象
    manager = new QNetworkAccessManager(this);
    //把网络访问完成的信号和槽函数绑定，信号的参数就是网页的响应（也就是网站返回给你的内容）
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));


    for(int j=0 ;j<23;j++)
    {
        if(pixmap[j].load(QString("%1.bmp").arg(j+1))==false)
        {
            qDebug()<<"图片加载失败";
        }
        else
        {
            qDebug()<<"图片加载成功";
        }

    }



}

weather::~weather()
{
    delete ui;
}



void weather::replyFinished(QNetworkReply *reply)
{
    QVariant ret =reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
  if(ret==200)//读取网页所有内容
     {
        QByteArray ByteArr=reply->readAll();
        QJsonDocument jsonDoc = QJsonDocument::fromJson(ByteArr);
        if(jsonDoc.isObject())
        {
            QJsonObject objRoot = jsonDoc.object();//获得文档对象
            if(objRoot.contains("date"))
            {
                QJsonValue val=objRoot.value("date");
                   CityInfo.time=val.toString();
                   qDebug()<<"date:"<<val.toString();
            }
            if(objRoot.contains("cityInfo"))
            {
                QJsonValue val=objRoot.value("cityInfo");
                if(val.isObject())
                {
                    QJsonObject cityInfoobj = val.toObject();//获得文档对象
                    if(cityInfoobj.contains("city"))
                    {
                        QJsonValue val=cityInfoobj.value("city");
                        CityInfo.city=val.toString();
                        qDebug()<<"city:"<<val.toString();

                    }

                    if(cityInfoobj.contains("citykey"))
                    {
                        QJsonValue val=cityInfoobj.value("citykey");
                        CityInfo.citykey=val.toString();
                        qDebug()<<"citykey:"<<val.toString();
                    }

                    if(cityInfoobj.contains("updateTime"))
                    {
                        QJsonValue val=cityInfoobj.value("updateTime");
                        CityInfo.updateTime=val.toString();
                        qDebug()<<"updateTime:"<<val.toString();

                    }
                }//if(val.isObject())
            }//if(objRoot.contains("cityInfo"))

            if(objRoot.contains("data"))
            {
                QJsonValue val=objRoot.value("data");
                if(val.isObject())
                {
                    QJsonObject Dataobj = val.toObject();//转为对象

                    if(Dataobj.contains("shidu"))
                    {
                        QJsonValue val=Dataobj.value("shidu");
                        CityInfo.shidu=val.toString();
                        qDebug()<<"shidu:"<<val.toString();
                    }


                    if(Dataobj.contains("pm25"))
                    {
                        QJsonValue val=Dataobj.value("pm25");
                        CityInfo.pm25=val.toInt();
                        qDebug()<<"pm25:"<<val.toInt();
                    }


                    if(Dataobj.contains("pm10"))
                    {
                        QJsonValue val=Dataobj.value("pm10");
                        CityInfo.pm10=val.toInt();
                        qDebug()<<"pm10:"<<val.toInt();
                    }


                    if(Dataobj.contains("quality"))
                    {
                        QJsonValue val=Dataobj.value("quality");
                        CityInfo.quality=val.toString();
                        qDebug()<<"quality:"<<val.toString();
                    }


                    if(Dataobj.contains("wendu"))
                    {
                        QJsonValue val=Dataobj.value("wendu");
                        CityInfo.wendu=val.toString();
                        qDebug()<<"wendu:"<<val.toString();
                    }



                    if(Dataobj.contains("ganmao"))
                    {
                        QJsonValue val=Dataobj.value("ganmao");
                        CityInfo.ganmao=val.toString();
                        qDebug()<<"ganmao:"<<val.toString();
                    }







                    if(Dataobj.contains("forecast"))
                    {
                        QJsonValue val=Dataobj.value("forecast");
                        if(val.isArray())
                        {
                            QJsonArray forecastArr = val.toArray();//把结果转为json数组
                            qDebug()<<forecastArr.size();
                            for(int j=0;j<forecastArr.size();j++)
                            {   oneDayData oneday;
                                //qDebug()<<"-----------";
                                if(forecastArr[j].isObject())
                                {
                                    QJsonObject forecastArrElm = forecastArr[j].toObject();//转为对象
                                    if(forecastArrElm.contains("date"))
                                    {
                                        QJsonValue val=forecastArrElm.value("date");

                                        oneday.date=val.toString();
                                        //qDebug()<<"date:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("high"))
                                    {
                                        QJsonValue val=forecastArrElm.value("high");

                                        oneday.high=val.toString();
                                        //qDebug()<<"high:"<<val.toString();
                                    }



                                    if(forecastArrElm.contains("low"))
                                    {
                                        QJsonValue val=forecastArrElm.value("low");

                                        oneday.low=val.toString();
                                        //qDebug()<<"low:"<<val.toString();
                                    }



                                    if(forecastArrElm.contains("ymd"))
                                    {
                                        QJsonValue val=forecastArrElm.value("ymd");

                                        oneday.ymd=val.toString();
                                        //qDebug()<<"date:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("week"))
                                    {
                                        QJsonValue val=forecastArrElm.value("week");

                                        oneday.week=val.toString();
                                        //qDebug()<<"week:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("sunrise"))
                                    {
                                        QJsonValue val=forecastArrElm.value("sunrise");

                                        oneday.date=val.toString();
                                        //qDebug()<<"sunrise:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("sunset"))
                                    {
                                        QJsonValue val=forecastArrElm.value("sunset");

                                        oneday.sunset=val.toString();
                                        //qDebug()<<"sunset:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("aqi"))
                                    {
                                        QJsonValue val=forecastArrElm.value("date");

                                        oneday.aqi=val.toInt();
                                        //qDebug()<<"aqi:"<<val.toInt();
                                    }


                                    if(forecastArrElm.contains("fx"))
                                    {
                                        QJsonValue val=forecastArrElm.value("fx");

                                        oneday.fx=val.toString();
                                        //qDebug()<<"fx:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("fl"))
                                    {
                                        QJsonValue val=forecastArrElm.value("fl");

                                        oneday.fl=val.toString();
                                        //qDebug()<<"date:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("type"))
                                    {
                                        QJsonValue val=forecastArrElm.value("type");

                                        oneday.type=val.toString();
                                        //qDebug()<<"type:"<<val.toString();
                                    }


                                    if(forecastArrElm.contains("notice"))
                                    {
                                        QJsonValue val=forecastArrElm.value("notice");

                                        oneday.notice=val.toString();
                                        //qDebug()<<"notice:"<<val.toString();
                                    }
                                }//if(forecastArr[j].isObject())
                                ui->city->setText(QString("%1").arg(CityInfo.city));
                                ui->ganmao->setText(QString("%1").arg(CityInfo.ganmao));
                                ui->pm10->setText(QString("%1").arg(CityInfo.pm10));
                                ui->pm25->setText(QString("%1").arg(CityInfo.pm25));
                                ui->shidu->setText(QString("%1").arg(CityInfo.shidu));
                                ui->wendu->setText(QString("%1").arg(CityInfo.wendu));
                                ui->time->setText(QString("%1").arg(CityInfo.time));

                                for(i=0;i<25;i++)//找第一天的照片
                                {
                                    if((j==0)&&(oneday.type==QString("%1").arg(weatherch[i])))
                                    {
                                        qDebug()<<"成功输出天气照片";
                                        ui->weatherpicture->setPixmap(pixmap[i]);
                                         weatherpicture[j]=i;
                                        break;
                                    }
                                    else if((j!=0)&&oneday.type==QString("%1").arg(weatherch[i]))
                                        {
                                          weatherpicture[j]=i;
                                        break;
                                        }

                                    else{
                                        weatherpicture[j]=24;
                                    }
                                }


                                icon.addPixmap(pixmap[weatherpicture[j]].scaled(100,100));
                                qDebug()<<weatherpicture[j];
                                QTableWidgetItem * pItem1 = new QTableWidgetItem(oneday.week);//星期
                                QTableWidgetItem * pItem2 = new QTableWidgetItem();//天气图片
                                pItem2->setIcon(icon);
                                pItem2->setText(QString("%1").arg(oneday.type));
                                QTableWidgetItem * pItem3 = new QTableWidgetItem(oneday.high);//最高温度
                                QTableWidgetItem * pItem4 = new QTableWidgetItem(oneday.low);//最低温度
                                //这里是把数组的元素数量作为表格的下标，因为数组中的元素数量和表格的元素是对应的
                                ui->tableWidget->setItem(0,vhalfmonthData.size(),pItem1);
                                ui->tableWidget->setItem(1,vhalfmonthData.size(),pItem2);
                                ui->tableWidget->setItem(2,vhalfmonthData.size(),pItem3);
                                ui->tableWidget->setItem(3,vhalfmonthData.size(),pItem4);

                                vhalfmonthData.append(oneday);
                            }//for(int j=0;j<forecastArr.size();j++)

                        }// if(val.isArray())
                    }//if(Dataobj.contains("forecast"))
                    if(Dataobj.contains("yesterday"))
                    {
                        QJsonValue val=Dataobj.value("yesterday");
                        if(val.isObject())
                        {
                             QJsonObject yesterdayobj = val.toObject();//转为对象
                            if(yesterdayobj.contains("date"))
                            {
                                QJsonValue val=yesterdayobj.value("date");

                                yesterday.date=val.toString();
                                //qDebug()<<"date:"<<val.toString();
                            }


                            if(yesterdayobj.contains("high"))
                            {
                                QJsonValue val=yesterdayobj.value("high");

                                yesterday.high=val.toString();
                                //qDebug()<<"high:"<<val.toString();
                            }



                            if(yesterdayobj.contains("low"))
                            {
                                QJsonValue val=yesterdayobj.value("low");

                                yesterday.low=val.toString();
                                //qDebug()<<"low:"<<val.toString();
                            }



                            if(yesterdayobj.contains("ymd"))
                            {
                                QJsonValue val=yesterdayobj.value("ymd");

                                yesterday.ymd=val.toString();
                                //qDebug()<<"date:"<<val.toString();
                            }


                            if(yesterdayobj.contains("week"))
                            {
                                QJsonValue val=yesterdayobj.value("week");

                                yesterday.week=val.toString();
                                //qDebug()<<"week:"<<val.toString();
                            }


                            if(yesterdayobj.contains("sunrise"))
                            {
                                QJsonValue val=yesterdayobj.value("sunrise");

                                yesterday.date=val.toString();
                                //qDebug()<<"sunrise:"<<val.toString();
                            }


                            if(yesterdayobj.contains("sunset"))
                            {
                                QJsonValue val=yesterdayobj.value("sunset");

                                yesterday.sunset=val.toString();
                                //qDebug()<<"sunset:"<<val.toString();
                            }


                            if(yesterdayobj.contains("aqi"))
                            {
                                QJsonValue val=yesterdayobj.value("date");

                                yesterday.aqi=val.toInt();
                                //qDebug()<<"aqi:"<<val.toInt();
                            }


                            if(yesterdayobj.contains("fx"))
                            {
                                QJsonValue val=yesterdayobj.value("fx");

                                yesterday.fx=val.toString();
                                //qDebug()<<"fx:"<<val.toString();
                            }


                            if(yesterdayobj.contains("fl"))
                            {
                                QJsonValue val=yesterdayobj.value("fl");

                                yesterday.fl=val.toString();
                                //qDebug()<<"date:"<<val.toString();
                            }


                            if(yesterdayobj.contains("type"))
                            {
                                QJsonValue val=yesterdayobj.value("type");

                                yesterday.type=val.toString();
                                //qDebug()<<"type:"<<val.toString();
                            }


                            if(yesterdayobj.contains("notice"))
                            {
                                QJsonValue val=yesterdayobj.value("notice");

                                yesterday.notice=val.toString();
                                //qDebug()<<"notice:"<<val.toString();
                            }
                        }//if(val.isObject())
                    }// if(Dataobj.contains("yesterday"))
                }//if(val.isObject())
                }// if(objRoot.contains("Data"))
        }//if(jsonDoc.isObject())
    }else
    {
        qDebug()<<"提示，网页访问出错";
    }


}
